package com.netposa.toolkit;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class Utils {
   private static Logger logger = Logger.getLogger(Utils.class);

   public static String getFormatTime(Long timeStamp, String format) {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
      Date date = new Date();
      date.setTime(timeStamp);
      String formatDate = simpleDateFormat.format(date);
      return formatDate;
   }

   public static Long getLongTime(String dataStr, String format) throws ParseException {
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
      Date date = simpleDateFormat.parse(dataStr);
      long time = date.getTime();
      return time;
   }

   public static byte[] hexStringToBytes(String hexString) {
      if (hexString != null && !hexString.equals("")) {
         hexString = hexString.toUpperCase();
         int length = hexString.length() / 2;
         char[] hexChars = hexString.toCharArray();
         byte[] d = new byte[length];

         for(int i = 0; i < length; ++i) {
            int pos = i * 2;
            d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
         }

         return d;
      } else {
         return null;
      }
   }

   private static byte charToByte(char c) {
      return (byte)"0123456789ABCDEF".indexOf(c);
   }
}
